#!/bin/sh
chip=$(ipcinfo -c)
family=$(ipcinfo -f)
vendor=$(ipcinfo -v)

if [ ! "$chip" = "hi3536dv100" ]; then
	sensor=$(ipcinfo -s)
fi

basic() {
	# set boot delay
	fw_setenv bootdelay 0

	# free uart for telemetry
	sed -i "s!console::respawn:/sbin/getty -L console 0 vt100!#console::respawn:/sbin/getty -L console 0 vt100!g" /etc/inittab

	# tune socket buffer
	echo "net.core.rmem_default=512000" >> /etc/sysctl.conf
}

hisi_goke() {
	prefix=${family}
	if [ "$vendor" = "hisilicon" ]; then
		prefix="open"
	fi

	# remove unneeded modules
	sed -i "s!insmod ${prefix}_jpege.ko!#insmod ${prefix}_jpege.ko!g" "/usr/bin/load_${vendor}"
	sed -i "s!insmod ${prefix}_ive.ko save_power=0!#insmod ${prefix}_ive.ko save_power=0!g" "/usr/bin/load_${vendor}"
	sed -i "s!insmod ${prefix}_aio.ko!#insmod ${prefix}_aio.ko!g" "/usr/bin/load_${vendor}"
	sed -i "s!insmod ${prefix}_ai.ko!#insmod ${prefix}_ai.ko!g" "/usr/bin/load_${vendor}"
	sed -i "s!insmod ${prefix}_ao.ko!#insmod ${prefix}_ao.ko!g" "/usr/bin/load_${vendor}"
	sed -i "s!insmod ${prefix}_aenc.ko!#insmod ${prefix}_aenc.ko!g" "/usr/bin/load_${vendor}"
	sed -i "s!insmod ${prefix}_adec.ko!#insmod ${prefix}_adec.ko!g" "/usr/bin/load_${vendor}"
	sed -i "s!insmod ${prefix}_acodec.ko!#insmod ${prefix}_acodec.ko!g" "/usr/bin/load_${vendor}"

	# change fps 25 -> 30
	sed -i "s!Isp_FrameRate=25!Isp_FrameRate=30!g" /etc/sensors/imx307_i2c_2l_1080p.ini
	sed -i "s!Isp_FrameRate=25!Isp_FrameRate=30!g" /etc/sensors/smtsec_imx307_i2c_4l_1080p.ini
	sed -i "s!Isp_FrameRate=25!Isp_FrameRate=30!g" /etc/sensors/imx335_i2c_4M.ini
}

streamer() {
	if [ -f /usr/bin/majestic ]; then
		cli -s .isp.slowShutter disabled
		cli -s .image.contrast 50
		cli -s .image.luminance 50
		cli -s .video0.size 1920x1080
		cli -s .video0.fps 30
		cli -s .video0.bitrate 5120
		cli -s .video0.codec h264
		cli -s .video0.rcMode cbr
		cli -s .video0.gopSize 1.5

		# lowdelay support only imx307 sensor
		if [ "$sensor" = "imx307" ]; then
			cli -s .isp.lowDelay true
			#cli -s .video0.sliceUnits 4
		fi

		if [ "$sensor" = "imx335" ]; then
			cli -s .isp.drc 350
			cli -s .isp.sensorConfig /etc/sensors/imx335_i2c_4M.ini
		fi

		cli -s .hls.enabled false
		cli -s .netip.enabled false
		cli -s .jpeg.enabled false
		cli -s .outgoing.enabled true
		cli -s .outgoing.server udp://127.0.0.1:5600
	fi

	if [ -f /usr/bin/venc ]; then
		if [ "$sensor" = "imx335" ]; then
			sed -i "s!version=200_imx307B!version=300_imx335B!g" /etc/venc.conf
			sed -i "s!size=720p!size=1292x972!g" /etc/venc.conf
		fi
	fi
}

finish() {
	# complete tweaks and add flag
	touch /etc/system.ok
	echo "Restart system to apply fpv changes..."
	reboot
}

sigmastar() {
	sed -i "s|tunnel=false|tunnel=true|g" /etc/datalink.conf
	sed -i "s|serial=/dev/ttyAMA0|serial=/dev/ttyS2|g" /etc/telemetry.conf
	sed -i "s|Device = /dev/ttyAMA0|Device = /dev/ttyS2|g" /etc/mavlink.conf

	cli -s .isp.exposure 5
	cli -s .video0.codec h265
	cli -s .video0.size 1920x1080
	cli -s .video0.fps 60
	cli -s .video0.rcMode cbr
	cli -s .jpeg.enabled false
	cli -s .outgoing.enabled true
	cli -s .outgoing.server udp://127.0.0.1:5600

	touch /etc/system.ok
}

case "$1" in
	gk7205v200 | gk7205v210 | hi3516ev200)
		echo "Preparing system tweaks for ${chip}..."

		# set custom config
		if [ "$chip" = "gk7205v200" ] && [ "$sensor" = "imx307" ]; then
			echo "switch to custom sensor config"
			#cli -s .isp.sensorConfig /etc/sensors/60fps/720p_imx307_i2c_2l.ini
		fi

		# force set totalmem size
		fw_setenv totalmem 64M

		# fix osmem in bootargs
		bootargs=$(fw_printenv -n bootargs)
		if echo "$bootargs" | grep -q "32M"; then
			newbootargs="mem=\${osmem} "$(echo "$bootargs" | cut -d " " -f2-)
			fw_setenv bootargs "$newbootargs"
			fw_setenv osmem 40M
		fi

		# remove unneeded scripts
		rm -f /etc/init.d/S60crond /etc/init.d/S49ntpd /etc/init.d/S02klogd

		basic
		hisi_goke
		streamer
		finish
		;;

	gk7205v300 | hi3516ev300)
		echo "Preparing system tweaks for $chip..."

		# force set totalmem size
		fw_setenv totalmem 128M

		# increase osmem
		mem=$(fw_printenv -n osmem) && [ "$mem" = "32M" ] && fw_setenv osmem 64M && set_allocator cma || echo "Memory allocated for the system:" "$mem"

		# set sensor config for smtsec board
		if [ "$chip" = "gk7205v300" ] && [ "$sensor" = "imx307" ]; then
			cli -s .isp.sensorConfig /etc/sensors/smtsec_imx307_i2c_4l_1080p.ini
		fi

		basic
		hisi_goke
		streamer
		finish
		;;

	t31l | t31n | t31x | t31zx)
		echo "Preparing system tweaks for $chip..."
		basic
		streamer
		finish
		;;

	hi3536dv100)
		echo "Preparing system tweaks for $chip..."
		finish
		;;

	ssc33x)
		echo "Preparing system tweaks for $chip..."
		sigmastar
		;;

	*)
		echo "Usage: $0 {gk7205v200|gk7205v210|gk7205v300|hi3516ev200|hi3516ev300|t31l|t31n|t31x|t31zx|hi3536dv100|ssc33x}"
		exit 1
		;;
esac
